package com.pocketsweet.ui.uilib.wheelcity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pocketsweet.R;
import com.pocketsweet.ui.uilib.wheelcity.adapters.AbstractWheelTextAdapter;
import com.pocketsweet.ui.uilib.wheelcity.adapters.ArrayWheelAdapter;

public class wheelCity {
	private String cityTxt;
	private Context context;
	private View myView;
	private String porvinceStr;
	private String cityStr;
//	private String countryStr;

	public wheelCity(Context context) {
		initView(context);
	}

	private void initView(Context context) {
		this.context = context;
		View contentView = LayoutInflater.from(context).inflate(
				R.layout.wheelcity_cities_layout, null);
		final WheelView country = (WheelView) contentView
				.findViewById(R.id.wheelcity_country);
		country.setVisibleItems(3);
		country.setViewAdapter(new CountryAdapter(context));

		final String cities[][] = AddressData.CITIES;
		final String ccities[][][] = AddressData.COUNTIES;
		final WheelView city = (WheelView) contentView
				.findViewById(R.id.wheelcity_city);
		city.setVisibleItems(0);

//		final WheelView ccity = (WheelView) contentView
//				.findViewById(R.id.wheelcity_ccity);
//		ccity.setVisibleItems(0);//

		country.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateCities(city, cities, newValue);
				cityTxt = AddressData.PROVINCES[country.getCurrentItem()]
						+ " | "
						+ AddressData.CITIES[country.getCurrentItem()][city
								.getCurrentItem()];
//						+ " | "
//						+ TimeContinuedData.COUNTIES[country.getCurrentItem()][city
//								.getCurrentItem()][ccity.getCurrentItem()];
				porvinceStr = AddressData.PROVINCES[country.getCurrentItem()];
				cityStr = AddressData.CITIES[country.getCurrentItem()][city
						.getCurrentItem()];
//				countryStr = TimeContinuedData.COUNTIES[country.getCurrentItem()][city
//						.getCurrentItem()][ccity.getCurrentItem()];
			}

			@Override
			public void onChanged(WheelTimeView wheelTimeView, int oldValue,
					int newValue) {
				// TODO Auto-generated method stub
				
			}
		});

		city.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
//				updatecCities(ccities, country.getCurrentItem(),
//						newValue);
				cityTxt = AddressData.PROVINCES[country.getCurrentItem()]
						+ " | "
						+ AddressData.CITIES[country.getCurrentItem()][city
								.getCurrentItem()];
//						+ " | "
//						+ TimeContinuedData.COUNTIES[country.getCurrentItem()][city
//								.getCurrentItem()][ccity.getCurrentItem()];
				porvinceStr = AddressData.PROVINCES[country.getCurrentItem()];
				cityStr = AddressData.CITIES[country.getCurrentItem()][city
						.getCurrentItem()];
//				countryStr = TimeContinuedData.COUNTIES[country.getCurrentItem()][city
//						.getCurrentItem()][ccity.getCurrentItem()];
			}

			@Override
			public void onChanged(WheelTimeView wheelTimeView, int oldValue,
					int newValue) {
				// TODO Auto-generated method stub
				
			}
			
		});

//		ccity.addChangingListener(new OnWheelChangedListener() {
//			public void onChanged(WheelView wheel, int oldValue, int newValue) {
//				cityTxt = TimeContinuedData.PROVINCES[country.getCurrentItem()]
//						+ " | "
//						+ TimeContinuedData.CITIES[country.getCurrentItem()][city
//								.getCurrentItem()];
//						+ " | "
//						+ TimeContinuedData.COUNTIES[country.getCurrentItem()][city
//								.getCurrentItem()][ccity.getCurrentItem()];
//			}
//		});

		country.setCurrentItem(1);
		city.setCurrentItem(1);
//		ccity.setCurrentItem(1);
		this.myView = contentView;
	}

	public View getView() {
		return this.myView;
	}

	public String getText() {
		return this.cityTxt;
	}

	public String getProvinceText() {
		return this.porvinceStr;
	}

	public String getCityText() {
		return this.cityStr;
	}

//	public String getCountryText() {
//		return this.countryStr;
//	}

	/**
	 * Updates the city wheel
	 */
	private void updateCities(WheelView city, String cities[][], int index) {
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(
				context, cities[index]);
		adapter.setTextSize(18);
		city.setViewAdapter(adapter);
		city.setCurrentItem(0);
	}

	/**
	 * Updates the ccity wheel
	 */
	private void updatecCities(WheelView city, String ccities[][][], int index,
			int index2) {
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(
				context, ccities[index][index2]);
		adapter.setTextSize(18);
		city.setViewAdapter(adapter);
		city.setCurrentItem(0);
	}

	/**
	 * Adapter for countries
	 */
	private class CountryAdapter extends AbstractWheelTextAdapter {
		// Countries names
		private String countries[] = AddressData.PROVINCES;

		/**
		 * Constructor
		 */
		protected CountryAdapter(Context context) {
			super(context, R.layout.wheelcity_country_layout, NO_RESOURCE);
			setItemTextResource(R.id.wheelcity_country_name);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return countries.length;
		}

		@Override
		protected CharSequence getItemText(int index) {
			return countries[index];
		}
	}
}
